package com.project.estate.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.estate.commons.bean.ChargeItem;
import com.project.estate.commons.bean.Community;
import com.project.estate.commons.config.JsonResult;
import com.project.estate.commons.config.ResultTool;
import com.project.estate.commons.handler.*;
import com.project.estate.commons.mapper.CommunityMapper;
import com.project.estate.community.service.CommunityService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommunityServiceImpl extends ServiceImpl<CommunityMapper, Community> implements CommunityService {

    //增加
    @Override
    public JsonResult addCommunity(Community community) {
        boolean save = save(community);
        if (!save){
            throw new CommunityAddException("添加失败！");
        }

        return ResultTool.success("success");
    }

    //删除
    @Override
    public JsonResult deleteCommunity(String code_id) {
        UpdateWrapper<Community> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("code", code_id);
        updateWrapper.set("status", 1);
        boolean update = update(updateWrapper);
        if (!update) {
            throw new CommunityDeleteException("删除失败");
        }
        return ResultTool.success("success");

    }

    //修改
    @Override
    public JsonResult updateCommunity(String code_id, Community community) {

            UpdateWrapper<Community> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("code", code_id);
            updateWrapper.set("address",community.getAddress());
            updateWrapper.set("name",community.getName());
            updateWrapper.set("total_buildings",community.getTotalBuildings());
            updateWrapper.set("total_buildings", community.getTotalBuildings());
            updateWrapper.set("thumbnail",community.getThumbnail());
            updateWrapper.set("developer",community.getDeveloper());
            updateWrapper.set("status",community.getStatus());
            boolean update = update(updateWrapper);
            if (!update) {
                throw new CommunityUpdateException("修改失败");
            }
            return ResultTool.success("success");

    }

    //查询
    @Override
    public JsonResult findCommunityById(String code_id) {
        QueryWrapper<Community> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", code_id);
        List<Community> list = list(queryWrapper);
        if (list.isEmpty()) {
            throw new ChargeItemFindException("未查询到");
        }
        return ResultTool.success(list);

    }
}
